package com.wn.intercept;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author yellow Docter
 * @date 2022 -07-13
 * @desc
 *  拦截器
 *
 *  01  实现 HandlerInterceptor 接口
 *
 *  02 重写方法
 *
 *  03 需要配置
 */
@Component  //需要把拦截器 注册到ioc容器中
public class ProjectHandlerInterceptor implements HandlerInterceptor {

    /**
     * 在controller 方法执行之前操作
     *
     * @param request
     * @param response
     * @param handler       如果结果为true  放行 如果结果为false 拦截
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        System.out.println("ProjectHandlerInterceptor.preHandle  在controller 方法执行执行 ");

        return true;
    }

    /**
     * 在controller 的执行方法之后操作
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

        System.out.println("ProjectHandlerInterceptor.postHandle 在controller 方法执行===>之后 ");

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
